package com.zmn.oms.business.impl.log;

import com.zmn.common.constant.GlobalConsts;
import com.zmn.oms.business.interfaces.log.FcPartLogBService;
import com.zmn.oms.model.entity.log.FcPartLog;
import com.zmn.oms.model.entity.log.OrderLog;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 类描述：工单log
 *
 * @author lujia
 * @date 2018/12/27 20:18
 */
@Service
public class FcPartBServiceImpl implements FcPartLogBService {

    @Autowired
    MongoTemplate mongoTemplate;

    @Override
    public List<FcPartLog> findLogsByFcPartId(Integer fcOrderPartId) {
        Query query = new Query();
        Criteria criteria = new Criteria();
        criteria.and("fcOrderPartId").is(fcOrderPartId);
        query.addCriteria(criteria);
        query.with(Sort.by(Sort.Order.desc("createTime")));
        return mongoTemplate.find(query, FcPartLog.class);
    }

    @Override
    public void save(FcPartLog fcPartLog) {

        if (fcPartLog.getCreateTime() == null) {
            fcPartLog.setCreateTime(new Date());
        }

        if (StringUtils.isBlank(fcPartLog.getOperatorTypeName()) && fcPartLog.getOperatorType() != null) {
            fcPartLog.setOperatorTypeName(GlobalConsts.getOperatorTypeName(fcPartLog.getOperatorType()));
        }

        mongoTemplate.save(fcPartLog);
    }
}
